<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Autoreply extends CI_Controller {

	public function index()
	{
		is_login($this->session);
		
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
		);
				
		$type = getdata($this->input->get('type'), 0);
		$data['type'] = $type;
		
		$keyword = getdata($this->input->get('keyword'), '');
		$data['search_item'] = array(
			'keyword' => $keyword
		);
		
		$where_arr = array();
		$cond = array();
		if ($keyword){
			$where_arr[] = 'r.content like "%'.$keyword.'%" or m.title like "%'.$keyword.'%"';
		}
		if ($type){
			$where_arr[] = 'r.type=?';
			$cond[] = $type;
		}
		$where = implode(' and ', $where_arr);
		
		$this->load->model('Autoreply_model', 'Mautoreply');
		$re_list = $this->Mautoreply->get_rule_list($where, $cond);
		
		$material_type = $this->config->config['BASE_MATERIAL_TYPE'];
		$_all_type = $this->Mautoreply->get_type_list();
		$all_type = array();
		if ($_all_type){
			foreach ($_all_type as $_type){
				$all_type[$_type['id']] = $_type['label'];
			}
		}
		$data['all_type'] = $all_type;
		
		$autoreply_list = array();
		foreach ($re_list as $item){
			if (!isset($autoreply_list[$item['id']])){
				$autoreply_list[$item['id']] = array(
					'id' => $item['id'],
					'type' => isset($all_type[$item['rtype']]) ? $all_type[$item['rtype']] : '未分类',
					'rule_name' => $item['rule_name'],
					'material_type' => empty($item['type']) ? $material_type[6] : $material_type[$item['type']],
					'content' => $item['content'],
					'add_time' => $item['add_time'],
				);
			}
			if (empty($autoreply_list[$item['id']]['content']) && !empty($item['title'])){
				$autoreply_list[$item['id']]['content'] = $item['title'];
			}
			if (!empty($item['keyword'])){
				$autoreply_list[$item['id']]['keywords'][] = $item['keyword'];
			}
		}
		
		$data['autoreply_list'] = $autoreply_list;
		
		$this->load->view('autoreply', $data);
		
	}
	
	public function edit(){
		is_login($this->session);
		
		$this->load->model('Autoreply_model', 'Mautoreply');
		$all_material_type = $this->config->config['BASE_MATERIAL_TYPE'];
		$_all_type = $this->Mautoreply->get_type_list();
		$all_type = array();
		if ($_all_type){
			foreach ($_all_type as $_type){
				$all_type[$_type['id']] = $_type['label'];
			}
		}
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
			'all_material_type' => $all_material_type,
			'all_type' => $all_type
		);
		
		$nowtime = date('Y-m-d H:i:s');
		
		if ($this->input->post()){
			$id = getdata($this->input->post('id'));
			$_material_type = getdata($this->input->post('material_type'));
			if ($_material_type == 6){
				$_material_id = 0;
				$_content = getdata($this->input->post('content'));
			}else{
				$_material_id = getdata($this->input->post('material_id'), 0);
				$_content = '';
			}
			$data_rule = array(
				'rule_name' => getdata($this->input->post('rule_name')),
				'type' => getdata($this->input->post('type')),
				'material_id' => $_material_id,
				'material_type' => $_material_type,
				'content' => $_content,
				'add_time' => $nowtime
			);
			$data_keyword = $this->input->post('keyword');
			
			if (empty($id)){
				$rule_id = $this->Mautoreply->add_rule($data_rule);
				
				if ($rule_id && $data_keyword){
					foreach ($data_keyword as $_word){
						$this->Mautoreply->add_keyword(array($rule_id, getdata($_word), $nowtime));
					}
				}
			}else{
				$data_up = array(
					'rule_name' => $data_rule['rule_name'],
					'type' => $data_rule['type'],
					'material_id' => $data_rule['material_id'],
					'material_type' => $data_rule['material_type'],
					'content' => $data_rule['content']
				);
				$this->Mautoreply->update_rule($data_up, 'where id=?', array($id));

				$this->Mautoreply->del_keyword('rule_id=?', array($id));
				foreach ($data_keyword as $_word){
					$add_re = $this->Mautoreply->add_keyword(array($id, getdata($_word), $nowtime));
				}
			}
			redirect(BASEWEBSITE . 'autoreply');
		}
		
		$id = getdata($this->input->get('id'));
		if ($id){
			$data['id'] = $id;
			$this->load->model('Autoreply_model', 'Mautoreply');
			$re_reply = $this->Mautoreply->get_rule_list('r.id=?', array($id));
			
			foreach ($re_reply as $item){
				if (!isset($autoreply)){
					$autoreply = array(
						'id' => $item['id'],
						'rule_name' => $item['rule_name'],
						'type' => $item['rtype'],
						'material_id' => $item['material_id'],
						'material_type' => $item['material_type'],
						'content' => $item['content'],
						'add_time' => $item['add_time'],
						'material_id_arr' => explode(',', $item['material_id'])
					);
				}
				if (!empty($item['keyword'])){
					$autoreply['keyword'][] = $item['keyword'];
				}
			}
			$data['autoreply'] = $autoreply;
		}
		
		// 素材
		$this->load->model('Material_model', 'Mmaterial');
		$_material_list = $this->Mmaterial->get_list('multi_id=0');
		$material_list = array();
		if ($_material_list){
			foreach ($_material_list as $material){
				$material_list[$material['type']][] = $material;
			}
		}
		$data['material_list'] = $material_list;
		
		$this->load->view('autoreply_edit', $data);
		
	}
	
	public function del(){
		is_login($this->session);
		
		$id = getdata($this->input->get('id'));
		if ($id){
			$this->load->model('Autoreply_model', 'Mautoreply');
			$re_del_rule = $this->Mautoreply->del_rule('id=?', array($id));
			$re_del_keyword = $this->Mautoreply->del_keyword('rule_id=?', array($id));
		}
		
		$previous_url = $_SERVER['HTTP_REFERER'];
		redirect($previous_url);
	}
	
	public function typelist(){
		is_login($this->session);
		
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
		);
		
		$this->load->model('Autoreply_model', 'Mautoreply');
		$all_type = $this->Mautoreply->get_type_list();
		$data['all_type'] = $all_type;
		
		$this->load->view('rule_type', $data);
	}
	
	public function typeedit(){
		is_login($this->session);
		
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
		);
		
		if ($this->input->post()){
			$rule_type = getdata($this->input->post('rule_type'));
			if (!$rule_type){
				redirect(BASEWEBSITE . 'autoreply/typeedit/');
			}
			
			$data_rule_type = array(
				$rule_type,
				date('Y-m-d H:i:s')
			);
			
			$this->load->model('Autoreply_model', 'Mautoreply');
			$this->Mautoreply->add_type($data_rule_type);
			redirect(BASEWEBSITE . 'autoreply/typelist/');
		}
		
		$this->load->view('rule_type_edit', $data);
	}
	
}
